﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Dotnet.Utils.Utility.EncryptUtil
{
    /// <summary>
    /// Md5 帮助类
    /// </summary>
    public static class Md5Helper
    {
        /// <summary>
        /// Md5加密
        /// </summary>
        /// <param name="content">要加密的内容</param>
        /// <param name="isUpper">是否大写，默认小写</param>
        /// <param name="is16">是否是16位，默认32位</param>
        /// <returns>Md5值</returns>
        public static string ToMd5Hash(string content, bool isUpper = false, bool is16 = false)
        {
            return ToMd5Hash(Encoding.UTF8.GetBytes(content), isUpper, is16);
        }

        /// <summary>
        /// Md5加密
        /// </summary>
        /// <param name="content">要加密的内容</param>
        /// <param name="isUpper">是否大写，默认小写</param>
        /// <param name="is16">是否是16位，默认32位</param>
        /// <returns>Md5值</returns>
        public static string ToMd5Hash(byte[] content, bool isUpper = false, bool is16 = false)
        {
            using var md5 = MD5.Create();
            var result = md5.ComputeHash(content);
            string md5Str = BitConverter.ToString(result);
            md5Str = md5Str.Replace("-", "");
            md5Str = isUpper ? md5Str : md5Str.ToLower();
            return is16 ? md5Str.Substring(8, 16) : md5Str;
        }

        /// <summary>
        /// Md5加密
        /// </summary>
        /// <param name="stream">要加密的内容</param>
        /// <param name="isUpper">是否大写，默认小写</param>
        /// <param name="is16">是否是16位，默认32位</param>
        /// <returns>Md5值</returns>
        public static string ToMd5Hash(Stream stream, bool isUpper = false, bool is16 = false)
        {
            using var md5 = MD5.Create();
            var result = md5.ComputeHash(stream);
            string md5Str = BitConverter.ToString(result);
            md5Str = md5Str.Replace("-", "");
            md5Str = isUpper ? md5Str : md5Str.ToLower();
            return is16 ? md5Str.Substring(8, 16) : md5Str;
        }

        public static string Hash(string strText)
        {
            string result2;
            using (MD5 md = MD5.Create())
            {
                byte[] result = md.ComputeHash(Encoding.UTF8.GetBytes(strText));
                result2 = Md5Helper.ToString(result);
            }
            return result2;
        }

        public static string Hash(Stream stream)
        {
            stream.Position = 0L;
            string result2;
            using (MD5 md = MD5.Create())
            {
                byte[] result = md.ComputeHash(stream);
                result2 = Md5Helper.ToString(result);
            }
            return result2;
        }

        public static string Hash(byte[] bytes)
        {
            string result2;
            using (MD5 md = MD5.Create())
            {
                byte[] result = md.ComputeHash(bytes);
                result2 = Md5Helper.ToString(result);
            }
            return result2;
        }

        private static string ToString(byte[] result)
        {
            return BitConverter.ToString(result).Replace("-", string.Empty).ToLowerInvariant();
        }
    }
}